var $ = require("../../utils/Ajax.js");
Page({
  data: {
    // 所有图片
    files: [],

    // 是否隐藏上传图片
    ClickUploader: false,

    // 存放服务器返回的图片地址
    ServerFiles: [],
    
    TopImgLenght: 0,

    // 图片总数
    ImgContNum: 4,
    Name: null,
    Birthday: null,
    Birthday_: null,
    Constellation: null,
    Hometown: null,
    Profession: null,
    School: null,
    Emotion: null,
    Sign: null,
    // 删除图片按钮
    DelectImgStatus: false
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "编辑资料"
    }) 
    var UserInfor = JSON.parse(options.UserInfor);
    var Movies = JSON.parse(options.Movies);
    if (Movies.length >= 4){
      this.setData({
        ClickUploader: true
      })
    }
    if (Movies.length > 1){
      this.setData({
        DelectImgStatus: true
      })
    }
    this.setData({
      files: Movies,
      TopImgLenght: Movies.length,
      ServerFiles: Movies,
      Name: UserInfor.nickName,
      Birthday: UserInfor.birthday,
      Birthday_: UserInfor.birthday0,
      Constellation: UserInfor.constellation,
      Hometown: UserInfor.hometown,
      Profession: UserInfor.profession,
      School: UserInfor.school,
      Emotion: UserInfor.emotion,
      Sign: UserInfor.sign
    })
  },

  onShow: function () {

  },

  // 上传图片
  chooseImage: function (e) {
    var that = this;
    var ImgContNum = that.data.ImgContNum;
    wx.chooseImage({
      count: ImgContNum,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 将每次上传图片的路径追加到files数组里面
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        // 总共可以上传6张图片减去本次上传的数量,就等于下次可以上传的数量
        var ImgNum = 4 - that.data.files.length;
        that.setData({
          ImgContNum: ImgNum
        })
        // 判断以经上传的照片大于或等于6张 就隐藏添加图片按钮
        if (that.data.files.length >= 4) {
          that.setData({
            ClickUploader: true
          });
        }
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        // 默认第一次上传图片的时候 I 等于0
        // 第二次上传的时候 I 就等于上次上传图片的数量
        for (var i = that.data.TopImgLenght; i < that.data.files.length; i++) {
          wx.uploadFile({
            url: 'https://sl.sx-sss.com/api/upload',
            filePath: that.data.files[i],
            name: 'file',
            success: function (res) {
              wx.hideToast();
              that.setData({
                ServerFiles: that.data.ServerFiles.concat('https://sl.sx-sss.com/' + JSON.parse(res.data).d.filename)
              })
              var Serverfiles = that.data.ServerFiles;
              if (Serverfiles.length > 1){
                that.setData({
                  DelectImgStatus: true
                })
              }else{
                that.setData({
                  DelectImgStatus: false
                })
              }
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 1500
              });
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
        }
        // 将图片的数量赋给 TopImgLenght 以便第二次上传图片的 I
        that.setData({
          TopImgLenght: that.data.files.length
        })
      }
    })
  },

  // 单击图片放大
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  // 点击删除图片
  DelectImg: function(e){
    var Index = e.currentTarget.dataset.index_;
    var Files = this.data.files;
    var ServerFiles = this.data.ServerFiles;
    Files.splice(Index, 1);
    ServerFiles.splice(Index, 1);
    this.setData({
      files: Files,
      ServerFiles: ServerFiles
    })
    if (Files.length > 1) {
      this.setData({
        DelectImgStatus: true
      })
    } else {
      this.setData({
        DelectImgStatus: false
      })
    }
  },

  // 编辑用户昵称
  Name: function (e){
    var Name = e.detail.value;
    this.setData({
      Name: Name
    })
  },

  // 编辑用户出生日期
  Birthday: function (e){
    var Birthday = e.detail.value;
    this.setData({
      Birthday: Birthday
    })
  },

  // 编辑用户生日
  Birthday_: function (e){
    var Birthday_ = e.detail.value;
    this.setData({
      Birthday_: Birthday_
    })
  },

  // 编辑用户星座
  Constellation: function (e){ 
    var Constellation = e.detail.value;
    this.setData({
      Constellation: Constellation
    })
  },

  // 编辑用户家乡
  Hometown: function (e){
    var Hometown = e.detail.value;
    this.setData({
      Hometown: Hometown
    })
  },

  // 编辑用户职业
  Profession: function (e){
    var Profession = e.detail.value;
    this.setData({
      Profession: Profession
    })
  },

  // 编辑用户学校
  School: function (e){ 
    var School = e.detail.value;
    this.setData({
      School: School
    })
  },

  // 编辑用户情感状态
  Emotion: function (e){ 
    var Emotion = e.detail.value;
    this.setData({
      Emotion: Emotion
    })
  },

  // 编辑用户个性签名
  Sign: function (e){ 
    var Sign = e.detail.value;
    this.setData({
      Sign: Sign
    })
  },

  // 提交用户信息
  EditUserInfor: function(){
    var that = this;
    var ServerFiles = that.data.ServerFiles;
    var Name = that.data.Name;
    var Birthday = that.data.Birthday;
    var Birthday_ = that.data.Birthday_;
    var Constellation = that.data.Constellation;
    var Hometown = that.data.Hometown;
    var Profession = that.data.Profession;
    var School = that.data.School;
    var Emotion = that.data.Emotion;
    var Sign = that.data.Sign;
    if (Name == '' || Name == null || Birthday == '' || Birthday == null || Birthday_ == '' || Birthday_ == null || Constellation == '' || Constellation == null || Hometown == '' || Hometown == null || Profession == '' || Profession == null || School == '' || School == null || Emotion == '' || Emotion == null || Sign == '' || Sign == null){
      wx.showToast({
        title: "用户信息不能为空",
        icon: 'none',
        duration: 1500
      })
    }else{
      $.Ajax("api/edit","POST",{
        avatarUrl: ServerFiles[0],
        album: ServerFiles,
        nickName: Name,
        birthday: Birthday,
        birthday0: Birthday_,
        constellation: Constellation,
        hometown: Hometown,
        profession: Profession,
        school: School,
        emotion: Emotion,
        sign: Sign
      },function(res){
        if(res.s){
          $.Ajax("api/uindex", "GET", {}, function (res) {
            if (res.s) {
              wx.setStorageSync('UserInfor', res.d.user);
              wx.showToast({
                title: res.m,
                icon: 'none',
                duration: 1500
              })
              setTimeout(function(){
                wx.navigateBack({
                  delta: 1
                })
              },2000)
            } else {
              wx.showToast({
                title: res.m,
                icon: 'none',
                duration: 1500
              })
            }
          })
        } else {
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 1500
          })
        }
      })
    }
  }
})